package cn.tedu;

import java.sql.*;

public class Demo03 {
    public static void main(String[] args) throws SQLException {
        //1. 获取数据库连接对象 导包java.sql    抛出异常
        Connection conn =
                DriverManager.getConnection("jdbc:mysql://localhost:3306/empdb?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false",
                        "root","root");
        //创建执行SQL语句的对象
        Statement s = conn.createStatement();
        //执行插入数据的SQL语句
//        s.executeUpdate("INSERT INTO emp(name) VALUES ('Tom')");
        //执行修改数据的SQL
//        s.executeUpdate("UPDATE emp SET name='Jerry' WHERE name='Tom'");
        //执行删除数据的SQL
        s.executeUpdate("DELETE FROM emp WHERE name='Jerry'");
        //查询数据
        ResultSet rs = s.executeQuery("SELECT name,sal FROM emp");
        //从结果集对象中遍历出查询到的数据
        while (rs.next()){
            //通过查询到的字段名称获取数据
//            String name = rs.getString("name");
//            double sal = rs.getDouble("sal");
            //通过查询到的字段位置获取数据
            String name = rs.getString(1);
            double sal = rs.getDouble(2);
            System.out.println(name+":"+sal);
        }
        System.out.println("执行完成!");
        //关闭资源
        conn.close();
    }
}
